﻿using Pipes.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Pipes.Code;

namespace Pipes.UnitTest
{
    
    
    /// <summary>
    ///This is a test class for DataExtensionsTest and is intended
    ///to contain all DataExtensionsTest Unit Tests
    ///</summary>
  [TestClass()]
  public class DataExtensionsTest
  {


    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
      get
      {
        return testContextInstance;
      }
      set
      {
        testContextInstance = value;
      }
    }

    #region Additional test attributes
    // 
    //You can use the following additional attributes as you write your tests:
    //
    //Use ClassInitialize to run code before running the first test in the class
    //[ClassInitialize()]
    //public static void MyClassInitialize(TestContext testContext)
    //{
    //}
    //
    //Use ClassCleanup to run code after all tests in a class have run
    //[ClassCleanup()]
    //public static void MyClassCleanup()
    //{
    //}
    //
    //Use TestInitialize to run code before running each test
    //[TestInitialize()]
    //public void MyTestInitialize()
    //{
    //}
    //
    //Use TestCleanup to run code after each test has run
    //[TestCleanup()]
    //public void MyTestCleanup()
    //{
    //}
    //
    #endregion

    /// <summary>
    ///A test for ReplaceSqlParams
    ///</summary>
    [TestMethod()]
    public void ReplaceSqlParamsTest1()
    {
      var sql = "select first [with a @weird name], second \"another @bad name\" -- @explanation of stuff\r\nfrom a_thing\r\n/* ignore @more stuff\r\n   that extends for a while */\r\nwhere (first = @valid and second = @valid2) or first = (@valid3 and second = @valid2)\r\norder by 1,2";
      var expected = "select first [with a @weird name], second \"another @bad name\" -- @explanation of stuff\r\nfrom a_thing\r\n/* ignore @more stuff\r\n   that extends for a while */\r\nwhere (first = null and second = 3) or first = ('value''s to test' and second = 3)\r\norder by 1,2";

      using (var w = new System.IO.StringWriter())
      {
        using (var writer = new Pipes.Data.SqlServerTextWriter(w))
        {
          DataExtensions.ReplaceSqlParams(writer, sql, '@', (p) => {
            switch (p)
            {
              case "valid":
                writer.StringValue(null);
                break;
              case "valid2":
                writer.NumberValue(3);
                break;
              case "valid3":
                writer.StringValue("value's to test");
                break;
            }
          });
        }
        Assert.AreEqual(expected, w.ToString());
      }
    }

    /// <summary>
    ///A test for ReplaceSqlParams
    ///</summary>
    [TestMethod()]
    public void ReplaceSqlParamsTest2()
    {
      var sql = "INSERT INTO innovator._PART_NUMBERS\r\nOUTPUT INSERTED.*\r\nSELECT \r\n    MAX(part_group) part_group\r\n  , MIN(case when p.part_group is null and n.num <> 0 then num ELSE 100000 end) pn_segment_2\r\n  , MAX(pad_to) pad_to\r\n  , @id locked_by_id\r\n  , GETUTCDATE() locked_on\r\n  , @lock_type lock_type\r\nFROM innovator._NUMBERS n\r\nleft JOIN innovator._part_numbers p\r\non p.pn_segment_2 = n.num\r\nand p.PART_GROUP = @part_group";
      var expected = "INSERT INTO innovator._PART_NUMBERS\r\nOUTPUT INSERTED.*\r\nSELECT \r\n    MAX(part_group) part_group\r\n  , MIN(case when p.part_group is null and n.num <> 0 then num ELSE 100000 end) pn_segment_2\r\n  , MAX(pad_to) pad_to\r\n  , null locked_by_id\r\n  , GETUTCDATE() locked_on\r\n  , 'M' lock_type\r\nFROM innovator._NUMBERS n\r\nleft JOIN innovator._part_numbers p\r\non p.pn_segment_2 = n.num\r\nand p.PART_GROUP = '905'";

      using (var w = new System.IO.StringWriter())
      {
        using (var writer = new Pipes.Data.SqlServerTextWriter(w))
        {
          DataExtensions.ReplaceSqlParams(writer, sql, '@', (p) =>
          {
            switch (p)
            {
              case "id":
                writer.StringValue(null);
                break;
              case "lock_type":
                writer.StringValue("M");
                break;
              case "part_group":
                writer.StringValue("905");
                break;
            }
          });
        }
        Assert.AreEqual(expected, w.ToString());
      }
    }

  }
}
